/**
 * This class embodies the project list.
 */
package org.hackystat.ui.projectviewer.client;

import java.util.Iterator;

import org.hackystat.ui.projectviewer.client.common.ProjectParcel;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * This class embodies the project list.
 * @author David J. Nickles
 *
 */
public class ProjectListPanel extends Composite implements ClickListener, WindowResizeListener {

  private VerticalPanel vp;
  private HorizontalPanel hp;
  private Tree root;
  private Session session;
  private int oddoreven;
  
  /**
   * Constructs a new ProjectsList and populates it from the current session.
   * @param session the session
   */
  public ProjectListPanel(Session session) {
    super();
    this.session = session;
    oddoreven = 0;
    this.vp = new VerticalPanel();
    this.vp.setStyleName(Styles.PANELSTYLE);
    this.vp.setWidth(Integer.toString(Window.getClientWidth() - 42) + Styles.PIXELSUFFIX);
    this.hp = new HorizontalPanel();
    //hp.setWidth("300px");
    hp.setWidth(Integer.toString(Window.getClientWidth() - 42) + Styles.PIXELSUFFIX);
    Label projectsListLable = new Label("Projects List:");
    hp.add(projectsListLable);
    hp.setCellHorizontalAlignment(projectsListLable, HasHorizontalAlignment.ALIGN_LEFT);
    HorizontalPanel bhp = new HorizontalPanel();
    Button button = new Button("New");
    button.addClickListener(this);
    bhp.add(button);
    hp.add(bhp);
    hp.setCellHorizontalAlignment(bhp, HasHorizontalAlignment.ALIGN_RIGHT);
    this.vp.add(hp);
    this.root = new Tree();
    Iterator it = session.getProjects().listIterator();
    while (it.hasNext()) {
      ProjectPanel projectPanel = new ProjectPanel((ProjectParcel)it.next(), session);
      this.root.addItem(projectPanel);
      if (oddoreven % 2 == 0) {
        projectPanel.addStyleName(Styles.ODDITEMSTYLE);
      } 
      else {
        projectPanel.addStyleName(Styles.EVENITEMSTYLE);
      }
      ++oddoreven;
    }
    this.vp.add(this.root);
    initWidget(this.vp);
    Window.addWindowResizeListener(this);
  }

  /** 
   * Listens for clicks on the UI elements.
   * @param sender the UI widget that is losing focus.
   */
  public void onClick(Widget sender) {
    RootPanel.get(CompositeWidget.PROJECTEDIT_ELEMENT).add(
      new ProjectEditPanel(this.session, null));
    removeFromParent();
  }

  /** 
   * Listens for user resizing of the window.
   * @param width of the adjusted window..
   * @param height of the adjusted window.
   */
  public void onWindowResized(int width, int height) {
    this.hp.setWidth(Integer.toString(width - 42) + Styles.PIXELSUFFIX);
    this.vp.setWidth(Integer.toString(Window.getClientWidth() - 42) + Styles.PIXELSUFFIX);
  }
}
